/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package management;

import entity.Customer;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author Cucnth
 */
public class MngCustomer {

    PreparedStatement ps = null;
    public static String sql = "select Customer_Id, Cus_Name, Cus_UserName, Cus_Departmental, Cus_Gender, Cus_Age, Cus_Phone, Cus_Address, Cus_Email, Cus_Status from Customer";

    public boolean addCustomer(Customer cus) {
        try {

            String sql1 = "insert into Customer values(?,?,?,?,?,?,?,?,?,?)";
            ps = MyConnection.getConnection().prepareStatement(sql1);
//            ps.setInt(1, cus.getId());
            ps.setString(1, cus.getFullName());
            ps.setString(2, cus.getUserName());
            ps.setString(3, cus.getPassWord());
            ps.setString(4, cus.getDepartmental());
            ps.setString(5, cus.getGender());
            ps.setInt(6, cus.getAge());
            ps.setString(7, cus.getPhone());
            ps.setString(8, cus.getAddress());
            ps.setString(9, cus.getEmail());
            ps.setString(10, cus.getStatus());
            JOptionPane.showMessageDialog(null, "Add customer successfully !");
            return (ps.executeUpdate() == 1);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Add customer error");
            ex.printStackTrace();
            return false;
        } finally {
            try {
                ps.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static boolean updateCustomer(String name, String department, String gender, int age, String phone, String address, String email, String status, int id) {
        String sql2 = "update Customer set Cus_Name=?, Cus_Departmental=?, Cus_Gender=?, Cus_Age=?, Cus_Phone=?, Cus_Address=?, Cus_Email=?, Cus_Status=? where Customer_Id=?";
        try {
            PreparedStatement ps = management.MyConnection.getConnection().prepareStatement(sql2);
            ps.setString(1, name);
            ps.setString(2, department);
            ps.setString(3, gender);
            ps.setInt(4, age);
            ps.setString(5, phone);
            ps.setString(6, address);
            ps.setString(7, email);
            ps.setString(8, status);
            ps.setInt(9, id);
            JOptionPane.showMessageDialog(null, "Edit Customer successfully !");

            return (ps.executeUpdate() == 1);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Edit Customer error!");
            ex.printStackTrace();
            return false;
        }
    }
    
      public static boolean updateMyInfo(String name, String pass, String gender, int age, String phone, String address, String userName) {
        String sql3 = "update Customer set Cus_Name=?, Cus_Pass=?, Cus_Gender=?, Cus_Age=?, Cus_Phone=?, Cus_Address=? where Cus_UserName=?";
        try {
            PreparedStatement ps = management.MyConnection.getConnection().prepareStatement(sql3);
            ps.setString(1, name);
            ps.setString(2, pass);
            ps.setString(3, gender);
            ps.setInt(4, age);
            ps.setString(5, phone);
            ps.setString(6, address);
            ps.setString(7, userName);
            JOptionPane.showMessageDialog(null, "Edit your's information successfully !");

            return (ps.executeUpdate() == 1);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Edit your's information error!");
            ex.printStackTrace();
            return false;
        }
    }
    

    public Boolean checkCus(String user) {
        try {
            Statement st = management.MyConnection.getConnection().createStatement();
            ResultSet rs = st.executeQuery("Select Cus_UserName from Customer");
            while (rs.next()) {
                if (rs.getString("Cus_UserName").equalsIgnoreCase(user)) {
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            return true;
        }

    }

    public Boolean checkManager(String user) {
        try {
            Statement st = management.MyConnection.getConnection().createStatement();
            ResultSet rs = st.executeQuery("Select Manager_UserName from Manager");
            while (rs.next()) {
                if (rs.getString("Manager_UserName").equalsIgnoreCase(user)) {
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            return true;
        }
    }

    public Boolean search(String user) {
        Boolean chCus = this.checkCus(user);
        Boolean chMa = this.checkManager(user);
        if (chCus == true && chMa == true) {
            return true;
        } else {
            return false;
        }

    }
}
